package com.tiancheng.trade.order.service.impl;

import com.tiancheng.trade.commom.web.http.PlatformRequest;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.order.bo.VipBO;
import com.tiancheng.trade.order.service.VipSystemService;
import com.tiancheng.trade.order.vo.app.res.VipCheckResVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: likailun
 * @Description:
 * @Date: create in 2022/7/27 4:08 PM
 */
@Service
@Slf4j
public class VipSystemServiceImpl implements VipSystemService {


    @Value(value = "${system.vip.domain}")
    private String vipDomain;
    @Resource
    private PlatformRequest wiiRequest;

    @Override
    public Result<VipCheckResVO> useBeamCheck(VipBO vipBO) {
        final Map<String, Object> params = new HashMap<>();
        params.put("uid", vipBO.getUid());
        params.put("order_id", vipBO.getOrderId());
        params.put("scene", vipBO.getOrderSource());
        params.put("order_amount", vipBO.getOrderAmount());
        params.put("use_beam", vipBO.getUseBeam());
        if (vipBO.getDiscount() != null && vipBO.getDiscount() > 0) {
            params.put("discount", vipBO.getDiscount());
        } else {
            params.put("discount", 100);
        }
        final List<Map<String, String>> productList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(vipBO.getProductList())) {
            for (VipBO.Product product : vipBO.getProductList()) {
                Map<String, String> p = new HashMap<>();
                p.put("product_id", product.getProductId());
                p.put("product_name", product.getProductName());
                productList.add(p);
            }
        }
        params.put("product_list", productList);
        try {
            log.info("会员云豆校验url:{}, params:{}", vipDomain + "/toS/user/order-check-reduce", JsonUtil.toJsonString(params));
            Result result = wiiRequest.jsonPostRequest(vipDomain + "/toS/user/order-check-reduce", JsonUtil.toJsonString(params), this.getTokenHeader());
            log.info("会员云豆校验返回:{},params:{}", result, JsonUtil.toJsonString(params));
            if (result.getRet() != 0) {
                return Result.fail("会员云豆校验失败", null);
            }
            Map<String, Object> resultMap = JsonUtil.fromJson(result.toString(), Map.class);
            Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
            return Result.success(new VipCheckResVO(MapUtils.getInteger(dataMap, "bean_reduce_amount"), MapUtils.getInteger(dataMap, "discount"), MapUtils.getString(dataMap, "level")));
        } catch (Exception e) {
			log.error("会员云豆校验失败，msg：{}", e.getMessage(), e);
			return Result.fail("会员云豆校验失败！", null);
        }
    }


    /**
     * 获取优惠券服务的token
     *
     * @return
     */
    private Header getTokenHeader() {
        String token = wiiRequest.getAccessToken();
        return new BasicHeader("token", token);
    }
}
